﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ReportPrinting;

namespace BSE.Windows.Test
{
    public partial class SummaryReportForm : BaseForm, IReportMaker
    {
        private bool initCompleted = false;

        public SummaryReportForm()
        {
            InitializeComponent();
        }

        private void SummaryReportForm_Load(object sender, EventArgs e)
        {
            int year = DateTime.Now.Year + 1;
            toolStripComboBoxYear.ComboBox.Items.Clear();
            for (int i = 0; i < 10; i++)
                toolStripComboBoxYear.ComboBox.Items.Add((year - i).ToString());
            toolStripComboBoxYear.SelectedIndex = 1;
            int month = DateTime.Now.Month;
            toolStripComboBoxMonth.ComboBox.Items.Clear();
            for (int i = 1; i < 12; i++)
            {
                toolStripComboBoxMonth.ComboBox.Items.Add(i.ToString());
            }
            toolStripComboBoxMonth.ComboBox.SelectedIndex = month - 1;

            List<YaopinAccount> ya = AnesthesiaBLL.GetMonthSummary(year, month);
            LoadSummaryList(ya);

            initCompleted = true;
        }

        private void LoadSummaryList()
        {
            int year = DateTime.Now.Year + 1 - toolStripComboBoxYear.SelectedIndex;
            int month = toolStripComboBoxMonth.SelectedIndex + 1;
            List<YaopinAccount> ya = AnesthesiaBLL.GetMonthSummary(year, month);
            LoadSummaryList(ya);
        }

        private void LoadSummaryList(List<YaopinAccount> ya)
        {
            listView1.Items.Clear();
            int i = 1;

            foreach (YaopinAccount y in ya)
            {
                ListViewItem lvit = new ListViewItem(i.ToString());
                lvit.ImageIndex = 0;
                lvit.Tag = y;
                i++;

                if (i % 2 == 1)
                    lvit.BackColor = Color.FromArgb(255, 192, 255, 255);
                else
                    lvit.BackColor = Color.FromArgb(255, 255, 255, 128);

                ListViewItem.ListViewSubItem lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinName;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinGuige;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.Yaopin.YaopinDanwei;
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumBefore.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumPurchase.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumUsageHospital.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumUsageOutpatient.ToString();
                lvit.SubItems.Add(lvsit);

                lvsit = new ListViewItem.ListViewSubItem();
                lvsit.Text = y.SumCurrent.ToString();
                lvit.SubItems.Add(lvsit);

                listView1.Items.Add(lvit);
            }
        }

        private void toolStripComboBoxYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (initCompleted)
                LoadSummaryList();
        }

        public void MakeDocument(ReportDocument reportDocument)
        {
            TextStyle.ResetStyles();

            TextStyle.Normal.Size = 10;
            TextStyle.Heading1.Size = 16;
            TextStyle.Heading1.Bold = false;
            TextStyle.TableHeader.BackgroundBrush = Brushes.White;// Brushes.Blue;
            TextStyle.TableHeader.Brush = Brushes.Black;//Brushes.LightGoldenrodYellow;
            TextStyle.TableHeader.MarginNear = 0.1f;
            TextStyle.TableHeader.MarginFar = 0.1f;
            TextStyle.TableHeader.MarginTop = 0.05f;
            TextStyle.TableHeader.MarginBottom = 0.05f;
            TextStyle.TableRow.MarginNear = 0.1f;
            TextStyle.TableRow.MarginFar = 0.1f;
            TextStyle.TableRow.MarginTop = 0.05f;

            ReportBuilder builder = new ReportBuilder(reportDocument);
            builder.StartLinearLayout(Direction.Vertical);

            Pen innerPen = new Pen(Color.Green, 0.02f);

            int year = DateTime.Now.Year + 1 - toolStripComboBoxYear.SelectedIndex;
            int month = toolStripComboBoxMonth.SelectedIndex + 1;
            builder.AddPageHeader(String.Empty, "麻精药品统计月报表\n" + year + " 年  " + month + " 月", "第 %p 页");
            //builder.AddPageHeader(String.Empty, "" + year + " 年  " + month + "月", String.Empty);

            //builder.AddText("Table with lines using default lines\n");
            // Following line sets up the pen used for lins for tables
            builder.DefaultTablePen = reportDocument.ThinPen;
            DataView dv = GetDataView();
            builder.AddTable(dv, true);
            builder.CurrentSection.HorizontalAlignment = ReportPrinting.HorizontalAlignment.Center;
            builder.AddColumn("YaopinName", "药品名称", 3.6f, true, true);
            builder.AddColumn("YaopinGuige", "规格", 2.0f, true, true);
            builder.AddColumn("YaopinDanwei", "单位", 1.8f, true, true);
            builder.AddColumn("SumBefore", "上月库存", 1.8f, true, true);
            builder.AddColumn("SumPurchase", "本月购进", 1.8f, true, true);
            builder.AddColumn("SumUsageHospital", "住院使用", 1.8f, true, true);
            builder.AddColumn("SumUsageOutpatient", "门诊使用", 1.8f, true, true);
            builder.AddColumn("SumCurrent", "本月库存", 1.8f, true, true);
            builder.AddColumn("Comment", "备注", 1.8f, true, true);
            builder.Table.OuterPens = reportDocument.NormalPen;

            /*
            builder.AddText("\nTable with lines using some customization\n");
            builder.AddTable(dv, true);
            builder.CurrentSection.HorizontalAlignment = HorizontalAlignment.Center;
            builder.AddColumn("FirstName", "First Name", 1.8f, false, true, HorizontalAlignment.Right);
            builder.AddColumn("LastName", "Last Name", 1.8f, true, true);
            builder.AddColumn("Birthdate", "Birthdate", 1.8f, true, true);
            builder.Table.InnerPenHeaderBottom = null; // no pen below header
            builder.Table.OuterPens = reportDocument.NormalPen;


            Pen colPen = new Pen(Color.Gray, 0.05f);
            TextStyle headerRow2 = new TextStyle(TextStyle.TableHeader);
            headerRow2.BackgroundBrush = null;
            headerRow2.Brush = Brushes.DarkBlue;

            builder.AddText("\nTable with lines using all customized stuff\n");
            builder.DefaultTablePen = null; // no default pen
            builder.AddTable(dv, true);
            builder.CurrentSection.HorizontalAlignment = HorizontalAlignment.Center;
            builder.AddColumn("FirstName", "First Name", 1.8f, true, true);
            builder.CurrentColumn.RightPen = colPen;
            builder.CurrentColumn.HeaderTextStyle = headerRow2;
            builder.AddColumn("LastName", "Last Name", 1.8f, true, true);
            builder.CurrentColumn.RightPen = colPen;
            builder.CurrentColumn.HeaderTextStyle = headerRow2;
            builder.AddColumn("Birthdate", "Birthdate", 1.8f, true, true);
            builder.CurrentColumn.HeaderTextStyle = headerRow2;
            builder.Table.InnerPenHeaderBottom = new Pen(Color.Red, 0.05f);
            builder.Table.OuterPens = reportDocument.NormalPen;
            builder.Table.InnerPenRow = reportDocument.ThinPen;

            // A table with 100% width, centered contents
            builder.AddText("\nTable with 100% width, centered contents\n");
            builder.DefaultTablePen = reportDocument.NormalPen;
            builder.AddTable(dv, true, 100f);
            builder.DefaultColumnAlignment = HorizontalAlignment.Center;
            builder.AddColumn("FirstName", "First Name", 2f, true, true);
            builder.AddColumn("LastName", "Last Name", 2f, true, true);
            builder.AddColumn("Birthdate", "Birthdate", 2f, true, true);
            builder.DefaultColumnAlignment = HorizontalAlignment.Left; // put it back

            builder.AddPageBreak();
            // A table with 75% width, one column a bit wider than others
            builder.AddText("\nTable with 75% width\n");
            builder.DefaultTablePen = reportDocument.ThinPen;
            builder.AddTable(dv, true, 75);
            builder.CurrentSection.HorizontalAlignment = HorizontalAlignment.Center;
            builder.AddColumn("FirstName", "First Name", 1.8f, true, true);
            builder.AddColumn("LastName", "Last Name", 1.8f, true, true);
            builder.AddColumn("Birthdate", "Birthdate With Long Title", 4f, true, true);

            // A table with 75% width, and some wide lines
            builder.AddText("\nTable with 75% width, and really fat lines\n");
            builder.DefaultTablePen = null; // no default pen
            builder.AddTable(dv, true, 75);
            Pen widePen = new Pen(Color.Beige, 0.3f);
            builder.CurrentSection.HorizontalAlignment = HorizontalAlignment.Center;
            builder.AddColumn("FirstName", "First Name", 2f, true, true);
            builder.CurrentColumn.RightPen = widePen;
            builder.CurrentColumn.HeaderTextStyle = headerRow2;
            builder.AddColumn("LastName", "Last Name", 2f, true, true);
            builder.CurrentColumn.RightPen = widePen;
            builder.CurrentColumn.HeaderTextStyle = headerRow2;
            builder.AddColumn("Birthdate", "Birthdate", 2f, true, true);
            builder.CurrentColumn.HeaderTextStyle = headerRow2;
            builder.Table.InnerPenHeaderBottom = new Pen(Color.Red, 0.05f);
            builder.Table.OuterPens = widePen;
            builder.Table.InnerPenRow = reportDocument.ThinPen;
            */

            builder.FinishLinearLayout();
        }

        private DataView GetDataView()
        {   
            DataTable dt = new DataTable();
            dt.Columns.Add("YaopinName");
            dt.Columns.Add("YaopinGuige");
            dt.Columns.Add("YaopinDanwei");
            dt.Columns.Add("SumBefore");
            dt.Columns.Add("SumPurchase");
            dt.Columns.Add("SumUsageHospital");
            dt.Columns.Add("SumUsageOutpatient");
            dt.Columns.Add("SumCurrent");
            dt.Columns.Add("Comment");

            foreach (ListViewItem item in listView1.Items)
            { 
                YaopinAccount ya = item.Tag as YaopinAccount;
                DataRow dr = dt.NewRow();
                dr["YaopinName"] = ya.Yaopin.YaopinName;
                dr["YaopinGuige"] = ya.Yaopin.YaopinGuige;
                dr["YaopinDanwei"] = ya.Yaopin.YaopinDanwei;
                dr["SumBefore"] = ya.SumBefore;
                dr["SumPurchase"] = ya.SumPurchase;
                dr["SumUsageHospital"] = ya.SumUsageHospital;
                dr["SumUsageOutpatient"] = ya.SumUsageOutpatient;
                dr["SumCurrent"] = ya.SumCurrent;
                dr["Comment"] = "";
                dt.Rows.Add(dr);
            }

            return dt.DefaultView;
        }
    }
}
